package com.rdc.jiajiadictionary.adapter;

import android.media.MediaPlayer;
import android.util.SparseBooleanArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;


import com.rdc.jiajiadictionary.R;
import com.rdc.jiajiadictionary.bean.Word;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class NotebookAdapter extends RecyclerView.Adapter<NotebookAdapter.NotebookViewHolder> {
    private List<Word> words;
    private OnItemClickListener onItemClickListener;
    private MediaPlayer mediaPlayer;
    private int visible;
    public static final int NO_CHECKBOX = -1;
    public static final int ALL_CHECKED = -2;
    private SparseBooleanArray mCheckStates;


    /**
     * 子项点击回调接口
     */
    public interface OnItemClickListener{
        void onItemClick(View view,int position);
        void onPlayClick(int position);
        void onLongClick(int position);
        void onCheckedChange(int position,boolean isChecked);
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener){
        this.onItemClickListener = onItemClickListener;
    }

    public NotebookAdapter(List<Word> words){
        this.words = words;
        mCheckStates=new SparseBooleanArray();
    }
    public NotebookAdapter(List<Word> words,int visible){
        this.words = words;
        this.visible = visible;
        mCheckStates=new SparseBooleanArray();
    }

    /**
     * 创建ViewHolder类
     */
    public class NotebookViewHolder extends RecyclerView.ViewHolder{
        private TextView name;
        private TextView partTv;
        private TextView phTv;
        private LinearLayout notebookItem;
        private ImageView playIv;
        private LinearLayout checkLl;
        private CheckBox checkBox;
        public NotebookViewHolder(@NonNull View itemView) {
            super(itemView);
            name = (TextView) itemView.findViewById(R.id.tv_wordName);
            partTv = (TextView) itemView.findViewById(R.id.tv_wordpart);
            phTv = (TextView) itemView.findViewById(R.id.tv_wordph_am);
            notebookItem = (LinearLayout) itemView.findViewById(R.id.ll_notebook_item);
            playIv = (ImageView) itemView.findViewById(R.id.iv_play_word);
            checkBox = (CheckBox) itemView.findViewById(R.id.checkbox);
            checkLl = (LinearLayout) itemView.findViewById(R.id.ll_check);
            if (visible == NO_CHECKBOX){
                checkLl.setVisibility(View.GONE);
            }else {
                checkLl.setVisibility(View.VISIBLE);
            }
        }
    }


    /**
     * 加载子项布局
     * @param parent
     * @param viewType
     * @return
     */
    @NonNull
    @Override
    public NotebookViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.notebook_recycle_item,parent,false);
        final NotebookViewHolder viewHolder = new NotebookViewHolder(view);
        return viewHolder;
    }

    /**
     * 加载子项数据，设置回调接口
     * @param holder
     * @param position
     */
    @Override
    public void onBindViewHolder(@NonNull NotebookViewHolder holder, int position) {
        holder.checkBox.setTag(position);
        Word word = words.get(position);
        holder.name.setText(word.getWord_name());
        holder.partTv.setText(word.getPart());
        holder.phTv.setText("美 /"+word.getPh_am()+"/");
        if (onItemClickListener != null){
            holder.notebookItem.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (visible != NO_CHECKBOX){
                        if (holder.checkBox.isChecked()){
                            holder.checkBox.setChecked(false);
                        }else {
                            holder.checkBox.setChecked(true);
                        }
                    } else {
                        onItemClickListener.onItemClick(v,position);
                    }
                }
            });
            holder.playIv.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    onItemClickListener.onPlayClick(position);
                }
            });
            holder.checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
//                    onItemClickListener.onCheckedChange(position,isChecked);
                    int pos= (int) buttonView.getTag();
                    if (isChecked){
                        //点击时将当前CheckBox的索引值和Boolean存入SparseBooleanArray中
                        mCheckStates.put(pos,true);
                    }else {
                        //否则将 当前CheckBox对象从SparseBooleanArray中移除
                        mCheckStates.delete(pos);
                    }
                    onItemClickListener.onCheckedChange(position,isChecked);
                }
            });
            //在mCheckStates找不到加入的position则将checkbox的checked设置为false
            holder.checkBox.setChecked(mCheckStates.get(position,false));

            holder.notebookItem.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                    onItemClickListener.onLongClick(position);
                    return true;
                }
            });

        }
        if (position == visible ){
            holder.checkBox.setChecked(true);
        }
        if (visible == ALL_CHECKED){
            holder.checkBox.setChecked(true);
        }
    }

    @Override
    public int getItemCount() {
        return words.size();
    }
}
